﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CodeGame
{
    class FiniteWorld : World
    {
        private Dictionary<Blocks, Texture2D> Textures = new Dictionary<Blocks, Texture2D>();

        private Blocks[,] WorldBlocks;

        const int SIZE = 80;
        const int TEXTURESIZE = 32;

        public FiniteWorld()
        {
            this.LoadTextures();

            this.WorldBlocks = new Blocks[SIZE, SIZE];
            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    this.WorldBlocks[x, y] = Blocks.Air;
                }
            }

            Singletons.CurrentWorld = this;
        }

        public override void LoadNetworkWorld(byte[] data)
        {
            byte[,] data2 = new byte[SIZE, SIZE];

            int i = 0;

            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    data2[x,y] = data[i];
                    i++;
                }
            }

            this.LoadWorld(data2);
        }

        private void LoadTextures()
        {
            this.Textures.Add(Blocks.Air, null);
            this.Textures.Add(Blocks.Grass, Singletons.Content.GetTexture("grass"));
            this.Textures.Add(Blocks.LongGrass, Singletons.Content.GetTexture("longGrass"));
        }

        public override void LoadWorld(byte[,] world)
        {
            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    this.WorldBlocks[x, y] = (Blocks)Convert.ToInt32(world[x, y]);
                }
            }
        }

        public override Blocks GetPointData(int x, int y)
        {
            return this.WorldBlocks[x, y];
        }

        public override void DrawWorld()
        {
            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    if (this.WorldBlocks[x,y] != Blocks.Air)
                    {
                        Singletons.Content.DrawImage(this.Textures[this.WorldBlocks[x, y]],
                            x * TEXTURESIZE + (int)this.CameraX, y * TEXTURESIZE + (int)this.CameraY, Color.White);
                    }
                }
            }
        }

    }
}
